import pymysql
from movie_search import *
from movie_search import movie_detail as mo_d
from movie_info_cn import *

def update1(str):
    # 打开数据库连接
    conn = pymysql.connect(host="139.9.219.187",port=3306,user="lh",passwd="159753",db="movie_big_data",charset='utf8')
    # 使用cursor()方法获取操作游标
    cursor = conn.cursor()
    #批量插入数据
    try:
        #豆瓣电影具体数据
        detail, comment = pachong_movie(geturl(str))

        movie_detail = mo_d(detail)
        list_movie_comment = movie_comment(comment)

        name = movie_detail[2]



        sql_getname = "select name from movie_info"
        cursor.execute(sql_getname)
        result = cursor.fetchall()




        result0 = [i[0] for i in result]


        if name not in result0:

            sql_movie_detail = "insert into  movie_info (imdb,image_path,name,eng_name,release_date,director,scriptwriter,actor,type,production_area,language,remark_count,synopsis,lasts_time,score,star_five,star_four,star_three,star_two,star_one) " \
                               "values(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"

            cursor.execute(sql_movie_detail,movie_detail)
            conn.commit()
            print("插入成功")

            sql_getid = "select id from movie_info where movie_info.imdb = '%s'" % (movie_detail[0])
            cursor.execute(sql_getid)
            movie_id = cursor.fetchone()

            #电影评论信息
            sql_movie_comment = "insert into  movie_comment (movie_id,imdb,avatar,name,post_time,title,content) " \
                               "values(%s,%s,%s,%s,%s,%s,%s)"
            list_movie_comment = [movie_id+i for i in list_movie_comment]
            cursor.executemany(sql_movie_comment,list_movie_comment)
            conn.commit()

            #电影类型

            sql_insert_type = 'insert into  movie_info_type (movie_id,type) values(%s,%s)'
            tuple_type = movie_id+(movie_detail[8],)
            print(tuple_type)
            cursor.execute(sql_insert_type, tuple_type)
            conn.commit()


        else:
            print("电影已存在")


    except Exception as e:
        print(e)
        conn.rollback()
    #关闭
    finally:
        conn.close()
        cursor.close()

if __name__ == '__main__':
    str = "保你平安"
    update1(str)